cc.Class({
    extends: cc.Component,

    properties: {
        zhuanpan: cc.Node
    },


    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.playerInfo = this.lobbyMain.playerInfo;
        this.showCoin = this.node.getChildByName("showCoin");
        this.showCoin.active = false;
        this.lobbyMain.openLoading();
    },

    start() {
        this.btnGO = this.zhuanpan.getChildByName("go1");
        this.btnGO.getComponent(cc.Button).interactable = false;
        this.getDailtdata();
        this.zhuanTar = this.zhuanpan.getChildByName("zp_pandi");
    },

    onClick(event, target) {
        if (target != "close2" && target != "topUp") {
            this.lobbyMain.playClickSound();
        }
        if (target == "close") {
            this.lobbyMain.hidePanel(this.node);
        } else if (target == "go") {  //开始摇
            this.sendDaily();
        } else if (target == "close2") {
            this.lobbyMain.hidePanel(this.showCoin);
        } else if (target == "topUp") {  //去充值
            this.lobbyMain.onClick(event, "shop");
        }
    },

    // 请求数据
    sendDaily() {
        this.btnGO.getComponent(cc.Button).interactable = false;
        let data = {
            action: "daily",
            playerId: this.playerInfo.playerId,
        }
        this.lobbyNet.sendMessage(data);
    },


    //获取每日转盘数据
    getDailtdata() {
        let data = {
            action: "getDaily",
            playerId: this.playerInfo.playerId,
        }
        this.lobbyNet.sendMessage(data);
    },

    //每日转盘数据返回
    onGetDailyRes(data) {
        this.lobbyMain.loading.active = false;
        if (data.respCode == "SUCCESS") {
            let info = data.data;
            if (info.is_receive) {
                this.lobbyMain.showTips("Your sign-in reward for today has been successfully claimed. Please come back tomorrow.");
                this.lobbyMain.hidePanel(this.node);
            }
            this.btnGO.getComponent(cc.Button).interactable = !info.daily;
            this.initZhuanpan(info);
        }
    },


    //初始化转盘
    initZhuanpan(info) {
        let zhuan = info.zhuan;
        let daily = info.daily
        let zhongjiang = this.zhuanpan.getChildByName("zhongjiang1");
        let bool = daily
        cc.tween(this.zhuanTar)
            .to(0.5, { angle: this.zhuanTar.angle + zhuan * 30 })  // 旋转动画，0.5秒完成
            .call(() => {
                zhongjiang.active = bool;
                if (bool) {
                    this.showCoin.getChildByName("label").getComponent(cc.Label).string = info.coin / this.playerInfo.ratio;
                    this.lobbyMain.showPanel(this.showCoin);
                }
            })
            .start();
    },

    //每日转盘返回
    onDaily(data) {
        this.node.getChildByName("close_2").getComponent(cc.Button).interactable = false;
        if (data.respCode == "SUCCESS") {
            let info = data.data;
            let rotateAngle = -360 * 10 - info.zhuan * 30;
            cc.tween(this.zhuanTar)
                .to(5, { angle: this.zhuanTar.angle - rotateAngle }, { easing: 'cubicInOut' }) // 先慢后快再慢
                .call(() => {
                    this.onZhuanWang(info);

                })
                .start();
        }
    },


    //转盘结束后
    onZhuanWang(info) {
        let zhongjiang = this.zhuanpan.getChildByName("zhongjiang1");
        zhongjiang.active = true;
        let anim = zhongjiang.getComponent(cc.Animation);
        let state = anim.getAnimationState("shan");
        state.wrapMode = cc.WrapMode.Normal;
        let playCount = 0;
        const maxCount = 3;
        anim.play("shan");
        anim.on('finished', () => {
            playCount++;
            if (playCount < maxCount) {
                anim.play("shan"); // 继续播放
            } else {
                // 播放完成3遍后的代码
                this.node.getChildByName("close_2").getComponent(cc.Button).interactable = true;
                this.showCoin.getChildByName("label").getComponent(cc.Label).string = info.coin;
                this.lobbyMain.showPanel(this.showCoin);
                // 移除监听，防止重复触发
                anim.off('finished');
            }
        });

    }

    // update (dt) {},
});
